﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.DTO;
using QuanLyKhachSan.BUS;

namespace QuanLyKhachSan
{
    public partial class frmQuanLyLoaiKhachHang : Form
    {
        // Khai bao bien toan cuc
        //bien luu tru cho biet Thao Tac(1: them, 2: update,3:Xoa)
        int thaoTac = 0;            
        // Danh sach loai khach hang co trong he thong
        List<string> dsLoaiKhachHang = new List<string>();
        //bien luu he thong hien tai
        private System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
        public frmQuanLyLoaiKhachHang()
        {
            InitializeComponent();
        }
        public void HienThiThongTin(int thaotac,string tenLoai)
        {
            // Cac button
            thaoTac = 0;
            btnHuy.Visible = false;
            btnLuu.Enabled = false;
            txtTenLoaiKH.Enabled = false;
            txtHeSo.Enabled = false;

            btnThoat.Visible = true;
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            //string tenLoai = txtTenLoaiKH.Text;
            DataTable dt = new DataTable();
            dt = LoaiKhachHangBUS.LayDSLoaiKHTheoTen(tenLoai);
            dgvDanhSachLoaiKH.DataSource = dt;

            if (thaotac == 1)
            {
                dsLoaiKhachHang.Add(tenLoai);
                cboLoaiKhachHang.Items.Add(tenLoai);
                cboLoaiKhachHang.Text = tenLoai;
            }
            else if (thaotac == 2)
            {
                dsLoaiKhachHang.Clear();
                cboLoaiKhachHang.Items.Clear();
                DataTable dt1 = new DataTable();
                dt1 = LoaiKhachHangBUS.LayDSLoaiKH();
               
                if (dt1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        DataRow dr = dt1.Rows[i];
                        string tenLoaiKH = dr["TenLoaiKhachHang"].ToString();

                        cboLoaiKhachHang.Items.Add(tenLoaiKH);
                        dsLoaiKhachHang.Add(tenLoaiKH);
                    }
                    cboLoaiKhachHang.Text = tenLoai;
                }
                
            }
            
            
        }
        private void frmQuanLyLoaiKhachHang_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;

            // Cac button
            thaoTac = 0;
            btnHuy.Visible = false;
            btnLuu.Enabled = false;
            txtTenLoaiKH.Enabled = false;
            txtHeSo.Enabled = false;

            btnThoat.Visible = true;
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            
            // Hien thi thong tin len DataGridView
            DataTable dt = new DataTable();
            dt = LoaiKhachHangBUS.LayDSLoaiKH();
            dgvDanhSachLoaiKH.DataSource = dt;

            // Xoa mang truoc khi them thong tin moi
            cboLoaiKhachHang.Items.Clear();
            dsLoaiKhachHang.Clear();
            // Hien len combobox
            

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    string tenLoaiKH = dr["TenLoaiKhachHang"].ToString();

                    cboLoaiKhachHang.Items.Add(tenLoaiKH);
                    dsLoaiKhachHang.Add(tenLoaiKH);
                }
                cboLoaiKhachHang.SelectedIndex = 0;
            }
            //su dung he thong en-US
            //oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
             System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            
        }
        // Kiem tra du lieu nhap vao
        public bool KiemTraDuLieuNhap()
        {
            if (string.IsNullOrEmpty(txtTenLoaiKH.Text) == true)
            {
                MessageBox.Show("Ten Loai Khach Hang Khong Duoc De Trong","Thong Bao");
                txtTenLoaiKH.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtHeSo.Text) == true)
            {
                MessageBox.Show("He So Nhan Khong Duoc De Trong","Thong Bao");
                txtHeSo.Focus();
                return false;
            }
            if (DTO.LoaiKhachHangDTO.KiemTraTenLoaiKhachHang(txtTenLoaiKH.Text) == false)
            {
                MessageBox.Show("Ten loai khach hang khong hop le ", "Thong bao ");
                txtTenLoaiKH.Focus();
                return false;
            }
            if (DTO.LoaiKhachHangDTO.KiemTraHeSoNhan(txtHeSo.Text) == false)
            {
                MessageBox.Show("He so nhan phai la kieu so", "Thong bao ");
                txtHeSo.Focus();
                return false;
            }
            return true;
        }
        // Kiem tra ten loai khach hang nhap vao da co trong he thong chua
        public bool KiemTraTheoTenLoaiKhachHang(string tenLoai)
        {
            foreach (string ten in dsLoaiKhachHang)
            {
                if (ten == tenLoai)
                {
                    MessageBox.Show("Ten Loai Khach Hang da co trong he thong ", "Thong bao ");
                    return false;
                }
            }
            return true;
        }
        public bool KiemTraCapNhat(string tenBD, string tenSau)
        {
            int i = 0;
            foreach (string ten in dsLoaiKhachHang )
            {
                if (ten == tenBD)
                {
                    dsLoaiKhachHang.RemoveAt(i);
                    break;
                }
                i++;
            }

            return KiemTraTheoTenLoaiKhachHang(tenSau);
        }
        public LoaiKhachHangDTO  LayThongTinTextbox()
        {
            LoaiKhachHangDTO loaiKhachHangdto = new LoaiKhachHangDTO();
            if (string.IsNullOrEmpty(txtMaLoaiKH.Text) == false)
            {
                loaiKhachHangdto.MaLoaiKH = int.Parse(txtMaLoaiKH.Text);
            }
            loaiKhachHangdto.TenLoai = txtTenLoaiKH.Text;
            loaiKhachHangdto.HeSoNhan=float.Parse(txtHeSo.Text );
            return loaiKhachHangdto;
        }
       
        private void btnThem_Click(object sender, EventArgs e)
        {
            thaoTac = 1;
            // Hien thi cac thong tin
            
            txtTenLoaiKH.Enabled = true;
            txtHeSo.Enabled = true;
            btnLuu.Enabled = true;
            btnHuy.Visible = true;
            // An cac thong tin
            btnThoat.Visible = false;
            btnThem.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            thaoTac = 2;
            // Hien thi cac thong tin
            
            txtTenLoaiKH.Enabled = true;
            txtHeSo.Enabled = true;
            btnLuu.Enabled = true;
            btnHuy.Visible = true;
            // An cac thong tin
            btnThoat.Visible = false;
            btnThem.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            bool kq = false;
            LoaiKhachHangDTO loaiKHdto = new LoaiKhachHangDTO();
            loaiKHdto.MaLoaiKH  = int.Parse(txtMaLoaiKH.Text);
            DialogResult result = MessageBox.Show("Ban chac chan muon xoa khong?", "Thong Bao", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                kq = LoaiKhachHangBUS.Delete(loaiKHdto );
            }
            if (kq == true)
            {
                MessageBox.Show("Xoa Loai Khach Hang thanh cong ", "Thong bao ");
                frmQuanLyLoaiKhachHang_Load(sender, e); // load lai form
            }
            else
            {
                MessageBox.Show("Xoa Loai Khach Hang khong thanh cong ", "Thong bao ");
                dgvDanhSachLoaiKH.Focus();
            }
            // An cac thong tin
            btnThoat.Visible = true ;
            btnThem.Enabled = true ;
            btnCapNhat.Enabled = true ;
            btnXoa.Enabled = true ;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            bool kq = false; ;
            if (KiemTraDuLieuNhap() == true)
            {
                // Lay thong tin tu form
                LoaiKhachHangDTO  loaiKhachHangdto = LayThongTinTextbox();

                // Them 1 loai phong
                if (thaoTac == 1)
                {
                    if (KiemTraTheoTenLoaiKhachHang(txtTenLoaiKH.Text) == false)
                    {
                        
                        txtTenLoaiKH.Focus();
                    }
                    else
                    {
                        kq = LoaiKhachHangBUS.Insert(loaiKhachHangdto);
                    }
                }
                else
                {
                    // Cap nhat thong tin loai phong
                    if (thaoTac == 2)
                    {
                        string tenBanDau = dgvDanhSachLoaiKH.SelectedRows[0].Cells[1].Value.ToString();
                        if (KiemTraCapNhat(tenBanDau,loaiKhachHangdto.TenLoai)==true  )
                        {
                            kq = LoaiKhachHangBUS.Update(loaiKhachHangdto );
                        }
                        else
                        {
                            txtTenLoaiKH.Focus();
                        }
                    }
                }
                if (kq == true)
                {

                    MessageBox.Show("Luu thong tin Loai Khach Hang thanh cong ", "Thong bao ");
                    HienThiThongTin(thaoTac ,loaiKhachHangdto.TenLoai);
                }
                else
                {
                    MessageBox.Show("Luu thong tin Loai Khach Hang khong thanh cong ", "Thong bao ");
                    txtTenLoaiKH.Focus();
                }

            }
        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
            // Hien thi cac thong tin
            btnLuu.Enabled = false;
            txtTenLoaiKH.Enabled = false;
            txtHeSo.Enabled = false;
            btnHuy.Visible = false;

            // An cac thong tin
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            btnThoat.Visible = true;

        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDanhSachLoaiKH_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDanhSachLoaiKH.SelectedRows.Count > 0)
            {
                txtMaLoaiKH.Text = dgvDanhSachLoaiKH.SelectedRows[0].Cells[0].Value.ToString();
                txtTenLoaiKH.Text = dgvDanhSachLoaiKH.SelectedRows[0].Cells[1].Value.ToString();
                txtHeSo.Text = dgvDanhSachLoaiKH.SelectedRows[0].Cells[2].Value.ToString();
            }
            else
            {
                txtMaLoaiKH.Text = "";
                txtTenLoaiKH.Text = "";
                txtHeSo.Text = "";
            }
        }

        private void cboLoaiKhachHang_TextChanged(object sender, EventArgs e)
        {
            int index = cboLoaiKhachHang.SelectedIndex;
            if (index != -1)
            {
                string tenTK = cboLoaiKhachHang.Text;
                DataTable dt = LoaiKhachHangBUS.LayDSLoaiKHTheoTen(tenTK);
                dgvDanhSachLoaiKH.DataSource = dt;
            }
        }

        private void frmQuanLyLoaiKhachHang_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
        }
    }
}